package com.hl.controller;

import com.hl.domain.Course.Course;
import com.hl.domain.Course.CourseProblem;
import com.hl.domain.Course.CourseTeacher;
import com.hl.domain.Course.Coursemark;
import com.hl.domain.PsychologicalTopic.PsychologicalTopicReply;
import com.hl.service.serviceImpl.CourseServiceImpl;
import com.hl.util.R;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class CourseController {

    @Autowired
    private CourseServiceImpl courseService;

    @GetMapping("/course/selectCourseById")
    public R selectCourseById(int i) {
        Course course = courseService.selectCourseById(i);
        if (course != null) {
            return R.success(course);
        } else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("/course/courseList")
    public R selectCourseList(int PageNum, int PageSize) {
        List<Course> courseList = courseService.selectCourseByPage(PageNum, PageSize);
        if (courseList != null) {
            return R.success(courseList);
        } else {
            return R.fail("查询失败");
        }
    }

    @PostMapping("/course/add")
    public R insertCourse(Course course) {
        int row = courseService.insertCourse(course);
        if (row > 0) {
            return R.success("添加成功");
        } else {
            return R.fail("添加失败");
        }
    }

    @PutMapping("/course/update")
    public R updateCourse(Course course) {
        System.out.println(course);
        int row = courseService.updateCourse(course);
        if (row > 0) {
            return R.success("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    @DeleteMapping("/course/delete")
    public R delereCourse(int i) {
        int row = courseService.deleteCourse(i);
        if (row > 0) {
            return R.success("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("/course/marks")
    public R selectAllCourseMark(int i) {
        List<Coursemark> coursemarkList = courseService.selectAllCourseMark(i);
        if (coursemarkList != null) {
            return R.success(coursemarkList);
        } else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("/course/teacher")
    public R selectCourseTeacherById(int i) {
        Map<String, Object> teacher = courseService.selectCourseTeacherById(i);
        if (teacher != null) {
            return R.success(teacher);
        } else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("/course/problem")
    public R selectCourseProblemById(int i) {
        List<CourseProblem> problems = courseService.selectCourseProblemById(i);
        if (problems != null) {
            return R.success(problems);
        } else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("/course/teacherlist")
    public R selectCourseTeacherList() {
        List<Map<String, Object>> list = courseService.selectAllCourseTeachers();
        if (list != null) {
            return R.success(list);
        } else {
            return R.fail("查询失败");
        }
    }

    @GetMapping("/course/courseListOptions")
    public R selectAllCourseByOptions(int PageNum, int PageSize, String courseName, Long teacherId) {
        List<Course> courses = courseService.selectALlCourseByOptions(PageNum, PageSize, courseName, teacherId);
        if (courses != null) {
            return R.success(courses);
        } else {
            return R.fail("查询失败");
        }
    }

    @PostMapping("/upload2233")
    public R upload(MultipartFile file) throws IOException {
        String path="E:\\upload\\";
        System.out.println("load----------------");

        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString()+ext;
        path += filename;
        file.transferTo(new File(path));
        return R.success("http://localhost:8080//down2233?fileName=E:/upload/"+filename);
    }

    @GetMapping("/down2233")
    public void down(HttpServletResponse response,String fileName) throws IOException {
        // 原生servlet
        File file = new File(fileName);
        InputStream inputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        FileCopyUtils.copy(inputStream,outputStream);

    }

    @GetMapping("/course/setprice")
    public R setPrice(Long courseId, Double price, Double discount){
        int i = courseService.setPrice(courseId,price,discount);
        if (i>0){
            return R.success("设置成功");
        }else {
            return R.success("设置失败");
        }
    }

    @GetMapping("/login")
    public R Login(String username,String password){
        Map<String,Object> map= courseService.Login(username,password);
        if (map!=null){
            return  R.success(map);
        }else {
            return R.fail("登录出错");
        }
    }


}
